/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DialogViajes.java
 *
 * Created on 22/01/2011, 01:54:43 PM
 */
package com.catosist.run.application.viaje;

import com.catosist.run.business.gestorpersona.GestorPersona;
import com.catosist.run.business.gestorruta.GestorRuta;
import com.catosist.run.business.gestorvehiculo.GestorVehiculo;
import com.catosist.run.business.gestorviaje.GestorViaje;
import com.catosist.run.dataaccess.domain.ViajeDao;
import com.catosist.run.services.PersonaDTO;
import com.catosist.run.services.RutaDTO;
import com.catosist.run.services.VehiculoDTO;
import com.catosist.run.services.ViajeDTO;
import java.util.Date;
import java.util.Iterator;
import javax.swing.DefaultComboBoxModel;

/**
 *
 * @author Carlos
 */
public class DialogViajes extends javax.swing.JDialog {

    /** Creates new form DialogViajes */
    public DialogViajes(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        
    }

    public DialogViajes(java.awt.Frame parent, boolean modal, int IdViaje) {
        super(parent, modal);
        initComponents();
        updateFields(IdViaje);
        this.viajeIndex = IdViaje;
        this.agregarChoferes();
        this.agregarRutas();
        this.agregarVehis();
    }

    public DialogViajes(java.awt.Frame parent, boolean modal,
            int IdViaje, GestorViaje gestorViaje,
            GestorRuta gestorRuta, GestorPersona gestorPersona,
            GestorVehiculo gestorVehiculo) {
        super(parent, modal);
        initComponents();
        this.gestorViaje = gestorViaje;
        this.gestorRuta = gestorRuta;
        this.gestorPersona = gestorPersona;
        this.gestorVehiculo = gestorVehiculo;

        this.agregarChoferes();
        this.agregarRutas();
        this.agregarVehis();

        this.viajeIndex = IdViaje;
        System.out.println("este es el ID  Q LLEGA " + viajeIndex);

        updateFields(IdViaje);

    }

//    public DialogViajes(java.awt.Frame parent, boolean modal, GestorViaje gestorViaje, GestorRuta gestorRuta, GestorPersona gestorPersona, GestorVehiculo gestorVehiculo) {
//        super(parent, modal);
//        initComponents();
//
//        this.gestorViaje = gestorViaje;
//        this.gestorRuta = gestorRuta;
//        this.gestorPersona = gestorPersona;
//        this.gestorVehiculo = gestorVehiculo;
//
//        this.agregarChoferes();
//        this.agregarRutas();
//        this.agregarVehis();
//    }
    public void setIndex(int viajeIndex, boolean inciado) {
        updateFields(viajeIndex);
        this.viajeIndex = viajeIndex;
        this.agregarChoferes();
        this.agregarRutas();
        this.agregarVehis();
        updateFields(viajeIndex);
    }

    private void updateFields(int i) {
        if (i == 0) {
            setTitle("Nuevo Viaje");
        } else {
            setTitle("Editar Viaje");
//            ViajeDTO viajeDTO = gestorViaje.getByIndex(i);
            ViajeDTO viajeDTO = gestorViaje.getById(i);
            this.cmbRuta.setSelectedIndex(gestorRuta.getIndexOf(viajeDTO.getRutaid()));
                this.cmbChofer.setSelectedIndex(gestorPersona.getIndexOf(viajeDTO.getPersonaId()));
            this.cmbVehi.setSelectedIndex(gestorVehiculo.getIndexOf(viajeDTO.getVehiculoId()));
            this.dtcFecha.setDate(new Date(viajeDTO.getFingreso().getTime())); //);
            this.txtAdeladnto.setText(String.valueOf(viajeDTO.getAdelanto()));
            gastototal = viajeDTO.getTotalgasto();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        cmbRuta = new javax.swing.JComboBox();
        cmbChofer = new javax.swing.JComboBox();
        cmbVehi = new javax.swing.JComboBox();
        btnAceptar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        dtcFecha = new com.toedter.calendar.JDateChooser("yyyy/MM/dd", "####/##/##", '_');
        lblStatus = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        txtAdeladnto = new javax.swing.JTextField();

        setTitle("Viaje");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setResizable(false);

        jPanel1.setName("jPanel1"); // NOI18N

        jLabel2.setText("Ruta:");
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText("Conductor:");
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText("Fecha:");
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText("Vehiculo: ");
        jLabel5.setName("jLabel5"); // NOI18N

        cmbRuta.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cmbRuta.setName("cmbRuta"); // NOI18N

        cmbChofer.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cmbChofer.setName("cmbChofer"); // NOI18N

        cmbVehi.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cmbVehi.setName("cmbVehi"); // NOI18N

        btnAceptar.setText("Aceptar");
        btnAceptar.setName("btnAceptar"); // NOI18N
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.setName("btnCancelar"); // NOI18N
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        dtcFecha.setDate(new java.util.Date());
        dtcFecha.setName("dtcFecha"); // NOI18N

        lblStatus.setBackground(new java.awt.Color(255, 51, 51));
        lblStatus.setForeground(new java.awt.Color(255, 51, 51));
        lblStatus.setText("...");
        lblStatus.setName("lblStatus"); // NOI18N

        jLabel1.setText("Adelanto:");
        jLabel1.setName("jLabel1"); // NOI18N

        txtAdeladnto.setText("0.0");
        txtAdeladnto.setName("txtAdeladnto"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblStatus, javax.swing.GroupLayout.DEFAULT_SIZE, 599, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cmbRuta, javax.swing.GroupLayout.PREFERRED_SIZE, 470, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(dtcFecha, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(cmbChofer, javax.swing.GroupLayout.Alignment.LEADING, 0, 179, Short.MAX_VALUE))
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jLabel5)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(cmbVehi, 0, 231, Short.MAX_VALUE))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(18, 18, 18)
                                        .addComponent(jLabel1)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txtAdeladnto, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(154, 154, 154)
                        .addComponent(btnAceptar)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancelar)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbRuta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbChofer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(cmbVehi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(dtcFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtAdeladnto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel1)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAceptar)
                    .addComponent(btnCancelar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        getAccessibleContext().setAccessibleParent(null);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
        // TODO add your handling code here:

        try {
            if (!this.dtcFecha.getDate().toString().isEmpty()) {

                ViajeDTO viajeDTO = new ViajeDTO();
                viajeDTO.setFingreso(this.dtcFecha.getDate());
//                viajeDTO.setIndex(viajeIndex);
                viajeDTO.setId(viajeIndex);
                viajeDTO.setPersonaId(gestorPersona.getIdAtIndex(cmbChofer.getSelectedIndex()));
                viajeDTO.setRutaid(gestorRuta.getIdAtIndex(cmbRuta.getSelectedIndex()));
                viajeDTO.setVehiculoId(gestorVehiculo.getIdAtIndex(cmbVehi.getSelectedIndex()));
                viajeDTO.setAdelanto(Float.valueOf(txtAdeladnto.getText()));
                viajeDTO.setTotalgasto(gastototal);
                gestorViaje.saveOrUpdate(viajeDTO);
                this.actualizado = true;
                this.setVisible(false);
            } else {
                lblStatus.setText("fecha invalida");
            }
        } catch (Exception e) {
            lblStatus.setText("fecha invalida");
            lblStatus.setText(e.getMessage());
        }
    }//GEN-LAST:event_btnAceptarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:his
        this.actualizado = false;
        this.setVisible(false);
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void agregarRutas() {

//        System.out.println(gestorViaje.get(1).getnPersona()+ "  si llego hasta aqui xD");
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        for (Iterator<RutaDTO> it = gestorRuta.getAll().iterator(); it.hasNext();) {
            RutaDTO rutaDTO = it.next();
            model.addElement(rutaDTO.getDescripcion());

        }
        this.cmbRuta.setModel(model);


    }

    private void agregarVehis() {

        DefaultComboBoxModel model = new DefaultComboBoxModel();
        for (Iterator<VehiculoDTO> it = gestorVehiculo.getAll().iterator(); it.hasNext();) {
            VehiculoDTO vehiculoDTO = it.next();
            model.addElement(vehiculoDTO.getMarca() +", "+ vehiculoDTO.getPlaca());
        }
        this.cmbVehi.setModel(model);

    }

    private void agregarChoferes() {
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        for (Iterator<PersonaDTO> it = gestorPersona.getAll().iterator(); it.hasNext();) {
            PersonaDTO personaDTO = it.next();
            model.addElement(personaDTO.getNombre() + " " + personaDTO.getAppaterno());
        }
        this.cmbChofer.setModel(model);
    }

    public boolean isActualizado() {
        return actualizado;
    }

    public void setActualizado(boolean actualizado) {
        this.actualizado = actualizado;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                DialogViajes dialog = new DialogViajes(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JComboBox cmbChofer;
    private javax.swing.JComboBox cmbRuta;
    private javax.swing.JComboBox cmbVehi;
    private com.toedter.calendar.JDateChooser dtcFecha;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JTextField txtAdeladnto;
    // End of variables declaration//GEN-END:variables
    private GestorPersona gestorPersona;
    private GestorRuta gestorRuta;
    private GestorVehiculo gestorVehiculo;
    private GestorViaje gestorViaje;
    private boolean actualizado;
    private int viajeIndex;
    private Float gastototal;
}
